package com.ops.wechat.core;

import com.ops.uitl.data.DataMap;
import com.ops.uitl.data.IData;
import org.apache.log4j.Logger;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.util.exception.ExceptionAnalyzer;
import org.apache.tapestry.util.exception.ExceptionDescription;
import org.apache.tapestry.util.exception.ExceptionProperty;


/**
 * wap应用公用异常显示页面.
 */
public abstract class WechatTimeOut extends WechatPage
{
	/**
	 * 日志记录器
	 */
	private Logger log = Logger.getLogger(WechatTimeOut.class);

	private ExceptionDescription[] _exceptions;

	public void initialize()
	{
		_exceptions = null;
	}

	public ExceptionDescription[] getExceptions()
	{
		return _exceptions;
	}

	public void setException(Throwable value)
	{
		ExceptionAnalyzer analyzer;

		analyzer = new ExceptionAnalyzer();

		_exceptions = analyzer.analyze(value);

//        log.error(_exceptions);
	}

	/**
	 * pageBeginRender
	 *
	 * @throws Exception
	 */
	public void pageBeginRender(PageEvent event)
	{
		try
		{
			log.debug("......WAPException pageBeginRender......");
			log.error("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
			log.error(" ");
			log.error(" 异常serialNumber ：" + ((WechatContext) pd.getContext()).getSerialNumber());
			log.error(" ");

			int size = _exceptions.length;
			for(int i=0; i<size; i++)
			{
				ExceptionDescription e = _exceptions[i];
				String eMessage = e.getMessage();
				if(eMessage.substring(eMessage.length()-4, eMessage.length()).equals("null"))
				{
					if(size>1)
						printNullPointerException(_exceptions[1]);

					return;
				}

				log.error("异常:" + eMessage);

				//用来记录日志 begin
				if(i>0)
				{
					IData param = new DataMap();
					param.put("ExceptionInfo", eMessage);
					pd.setData(param);
					//recordLog(false);
				}
				//用来记录日志 end

				String[] st = e.getStackTrace();
				if(st!=null)
				{
					int stSize = st.length;
					for(int j=0; j<stSize; j++)
					{
						log.error(st[j]);
					}
				}

				ExceptionProperty[] epro = e.getProperties();
				int proSize = epro.length;
				for(int j=0; j<proSize; j++)
				{
					ExceptionProperty ep = epro[j];
					log.error(ep.getName() + "|" + ep.getValue());
				}
				log.error(" ");
			}
			log.error("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

			return;
		}
		catch(Exception e)
		{
			log.error(e);
		}
	}

	/**
	 * log4j NullPointerException
	 */
	private void printNullPointerException(ExceptionDescription e)
	{
		String[] st = e.getStackTrace();
		if(st!=null)
		{
			int stSize = st.length;
			for(int j=0; j<stSize; )
			{
				if(j+1<stSize && st[j].startsWith("Caused by") && st[j+1].startsWith("com.esale"))
				{
					log.error(st[j]);
					for(int n=j+1; n<stSize; n++)
					{
						if(st[n].startsWith("com.esale"))
						{
							log.error(st[n]);
						}
						else
						{
							j = n+1;
							break;
						}
					}
				}
				else
				{
					log.debug(st[j]);
					j++;
				}
			}
		}

		log.error(" ");
		log.error("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
	}

}